class Solution {
public:
    int minSwap(vector<int>& nums1, vector<int>& nums2) {
        int n = nums1.size();
        vector<vector<int>> dp(n,vector<int>(2));
        dp[0][1] = 1;
        
        for(int i = 1;i < n;i++){   
            int x1 = nums1[i-1],y1 = nums1[i];
            int x2 = nums2[i-1],y2 = nums2[i];
            if(x1 < y1 && x2 < y2 && x1 < y2 && x2 < y1){
                dp[i][0] = min(dp[i-1][0],dp[i-1][1]);
                dp[i][1] = dp[i][0] + 1;
            }else if(x1 < y1 && x2 < y2){
                dp[i][0] = dp[i-1][0];
                dp[i][1] = dp[i-1][1] + 1;
            }else{
                dp[i][0] = dp[i-1][1];
                dp[i][1] = dp[i-1][0] + 1;
            }
        }
        return min(dp[n-1][0],dp[n-1][1]);
    }
};
